<template>
    <div class="profile-page">
        <h2>{{ $t('Account.Profile.Title') }}</h2>
        <div class="profile-content">
            <div class="profile-section">
                <h3>{{ $t('Account.Profile.PersonalInfo') }}</h3>
                <div class="form-group">
                    <label>{{ $t('Account.Profile.Name') }}</label>
                    <div class="input-wrapper">
                        <input type="text" disabled v-model="userInfo.nickname" :placeholder="$t('Account.Profile.NamePlaceholder')" />
                    </div>
                </div>
                <div class="form-group">
                    <label>{{ $t('Account.Profile.Email') }}</label>
                    <div class="input-wrapper">
                        <input type="email" disabled v-model="userInfo.email" :placeholder="$t('Account.Profile.EmailPlaceholder')" />
                    </div>
                </div>
                <!-- <div class="form-actions">
                    <button class="save-btn" @click="saveProfile">{{ $t('Account.Profile.SaveChanges') }}</button>
                </div> -->
            </div>

            <div class="profile-section">
                <h3>{{ $t('Account.Profile.Security') }}</h3>
                <div class="security-items">
                    <div class="security-item" v-if="userInfo.isSocialUser == 0">
                        <div class="security-info">
                            <div class="security-icon">
                               <img src="@/assets/pwd.png" alt="">
                            </div>
                            <div class="security-text">
                                <h4>{{ $t('Account.Profile.Password.Title') }}</h4>
                                <p>{{ $t('Account.Profile.Password.Description') }}</p>
                            </div>
                        </div>
                        <button class="action-btn" @click="showChangePassword = !showChangePassword">
                            {{ $t('Account.Profile.Password.Change') }}
                        </button>
                    </div>
                    <div class="security-item">
                        <div class="security-info">
                            <div class="security-icon">
                                <img src="@/assets/email.png" alt="">
                            </div>
                            <div class="security-text">
                                <h4>{{ $t('Account.Profile.EmailVerification.Title') }}</h4>
                                <p>{{ $t('Account.Profile.EmailVerification.Description') }}</p>
                            </div>
                        </div>
                        <span class="status-text verified">{{ $t('Account.Profile.EmailVerification.Verified') }}</span>
                    </div>
                </div>
            </div>

            <div class="dialog-mask" v-if="showChangePassword" @click.self="showChangePassword = false">
                <div class="dialog-container">
                    <div class="dialog-header">
                        <h3>{{ $t('Account.Profile.Password.DialogTitle') }}</h3>
                        <button class="close-btn" @click.stop="showChangePassword = false">×</button>
                    </div>
                    <div class="dialog-body">
                        <div class="password-form">
                            <div class="form-group">
                                <label>{{ $t('Account.Profile.Password.CurrentPassword') }}</label>
                                <div class="password-input-wrapper">
                                    <input 
                                        :type="showOldPassword ? 'text' : 'password'" 
                                        v-model="passwordForm.oldPassword" 
                                        :placeholder="$t('Account.Profile.Password.CurrentPasswordPlaceholder')" 
                                    />
                                    <!-- <span class="password-toggle" @click="showOldPassword = !showOldPassword">
                                        <img 
                                            :src="showOldPassword ? wlookImg : lookImg" 
                                            :alt="showOldPassword ? $t('Account.Profile.Password.HidePassword') : $t('Account.Profile.Password.ShowPassword')"
                                        />
                                    </span> -->
                                </div>
                            </div>
                            <div class="form-group">
                                <label>{{ $t('Account.Profile.Password.NewPassword') }}</label>
                                <div class="password-input-wrapper">
                                    <input 
                                        :type="showNewPassword ? 'text' : 'password'" 
                                        v-model="passwordForm.newPassword" 
                                        :placeholder="$t('Account.Profile.Password.NewPasswordPlaceholder')" 
                                    />
                                    <span class="password-toggle" @click="showNewPassword = !showNewPassword">
                                        <img 
                                            :src="showNewPassword ?  lookImg:  wlookImg" 
                                            :alt="showNewPassword ? $t('Account.Profile.Password.HidePassword') : $t('Account.Profile.Password.ShowPassword')"
                                        />
                                    </span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>{{ $t('Account.Profile.Password.ConfirmPassword') }}</label>
                                <div class="password-input-wrapper">
                                    <input 
                                        :type="showConfirmPassword ? 'text' : 'password'" 
                                        v-model="passwordForm.confirmPassword" 
                                        :placeholder="$t('Account.Profile.Password.ConfirmPasswordPlaceholder')" 
                                    />
                                    <span class="password-toggle" @click="showConfirmPassword = !showConfirmPassword">
                                        <img 
                                            :src="showConfirmPassword ?  lookImg : wlookImg" 
                                            :alt="showConfirmPassword ? $t('Account.Profile.Password.HidePassword') : $t('Account.Profile.Password.ShowPassword')"
                                        />
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="dialog-footer">
                        <button class="cancel-btn" @click="showChangePassword = false">{{ $t('Account.Profile.Password.Cancel') }}</button>
                        <button class="confirm-btn" @click="changePassword">{{ $t('Account.Profile.Password.Confirm') }}</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useI18n } from 'vue-i18n'
import encrypt from '@/utils/encrypt.js'
import { updateUserInfoApi, updatePasswordApi } from '@/api/login'
import wlookImg from '@/assets/header/wlook.png'
import lookImg from '@/assets/header/look.png'

const { t } = useI18n()
const userInfo = ref(encrypt.GetLocalData('userInfo'))
const router = useRouter()
const showChangePassword = ref(false)
const showOldPassword = ref(false)
const showNewPassword = ref(false)
const showConfirmPassword = ref(false)
const passwordForm = ref({
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
})

const validatePassword = (password) => {
    const minLength = 8;
    const hasNumber = /\d/;
    const hasLetter = /[a-zA-Z]/;

    if (password.length < minLength) {
        ElMessage.error(t('Account.Profile.Validation.PasswordLength'))
        return false;
    }
    if (!hasNumber.test(password) || !hasLetter.test(password)) {
        ElMessage.error(t('Account.Profile.Validation.PasswordRequirements'))
        return false;
    }
    return true;
}

const saveProfile = () => {
    // 验证邮箱格式
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    if (!emailRegex.test(userInfo.value.email)) {
        ElMessage.error(t('Account.Profile.Validation.EmailFormat'))
        return
    }

    // 验证名称
    if (!userInfo.value.nickname.trim()) {
        ElMessage.error(t('Account.Profile.Validation.NameRequired'))
        return
    }

    updateUserInfoApi({
        nickname: userInfo.value.nickname,
        username: userInfo.value.username,
        email: userInfo.value.email,
        avatar: userInfo.value.avatar
    }).then(res => {
        console.log(res, 'kaka');

        if (res.code === 200) {
            ElMessage.success(t('Account.Profile.Messages.SaveSuccess'))
        } else {
            ElMessage.error(t('Account.Profile.Messages.SaveFailed'))
        }
    })
}

const changePassword = () => {
    if (!passwordForm.value.oldPassword || !passwordForm.value.newPassword || !passwordForm.value.confirmPassword) {
        ElMessage.warning(t('Account.Profile.Validation.PasswordRequired'))
        return
    }
    if (passwordForm.value.newPassword !== passwordForm.value.confirmPassword) {
        ElMessage.error(t('Account.Profile.Validation.PasswordMismatch'))
        return
    }

    // 验证新密码规则
    if (!validatePassword(passwordForm.value.newPassword)) {
        return;
    }

    // 调用修改密码的 API
    updatePasswordApi({
        oldPassword: passwordForm.value.oldPassword,
        newPassword: passwordForm.value.newPassword,
        confirmPassword: passwordForm.value.confirmPassword
    }).then(res => {
        if (res.code == 0) {
            // 清除用户信息和 token
            encrypt.ClearLocalData('accessToken')
            encrypt.ClearLocalData('refreshToken')
            encrypt.ClearLocalData('userInfo')
            ElMessage.success(t('Account.Profile.Messages.PasswordChangeSuccess'))
            showChangePassword.value = false
            passwordForm.value = {
                oldPassword: '',
                newPassword: '',
                confirmPassword: ''
            }
            router.push('/login')
        } else {
            ElMessage.error(t('Account.Profile.Messages.PasswordIncorrect'))
        }
    })
}

const confirmDelete = () => {
    ElMessageBox.confirm(t('Account.Profile.Messages.DeleteAccountWarning'), t('Account.Profile.Messages.Warning'), {
        confirmButtonText: t('Account.Profile.Messages.Confirm'),
        cancelButtonText: t('Account.Profile.Messages.Cancel'),
        type: 'warning'
    })
        .then(() => {
            ElMessage.success(t('Account.Profile.Messages.DeleteAccountSuccess'))
        })
        .catch(() => {})
}
</script>

<style lang="scss" scoped>
.profile-page {
    height: 100%;
    color: #333;
    padding: 0 20px;

    h2 {
        font-size: 28px;
        margin-bottom: 32px;
        font-weight: 600;
    }
}

.profile-content {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.profile-section {
    background: white;
    padding: 32px;
    border-radius: 16px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);

    h3 {
        font-size: 20px;
        margin-bottom: 24px;
        color: #1a1a1a;
        font-weight: 600;
    }
}

.form-group {
    margin-bottom: 24px;

    label {
        display: block;
        margin-bottom: 10px;
        color: #4a4a4a;
        font-weight: 500;
        font-size: 15px;
    }

    .input-wrapper {
        display: flex;
        gap: 12px;

        input {
            flex: 1;
            padding: 12px 16px;
            border: 1px solid #e0e0e0;
            border-radius: 10px;
            font-size: 15px;
            transition: all 0.3s ease;

            &:focus {
                outline: none;
                border-color: #1a73e8;
                box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.1);
            }

            &::placeholder {
                color: #999;
            }
        }
    }
}

.form-actions {
    display: flex;
    justify-content: flex-start;
    margin-top: 32px;
}

.save-btn {
    padding: 12px 32px;
    background: #1a73e8;
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
        background: #1557b0;
        transform: translateY(-1px);
    }

    &:active {
        transform: translateY(0);
    }
}

.security-items {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.security-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 12px;
    transition: all 0.3s ease;

    &:hover {
        background: #f0f3f6;
    }

    .security-info {
        display: flex;
        align-items: center;
        gap: 16px;

        .security-icon {
            width: 40px;
            height: 40px;
            background: #e8f0fe;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #1a73e8;
            font-size: 20px;

            img{
                width: 20px;
                height: 20px;
            }
        }

        .security-text {
            h4 {
                font-size: 16px;
                margin-bottom: 4px;
                color: #1a1a1a;
                font-weight: 500;
            }

            p {
                color: #666;
                font-size: 14px;
            }
        }
    }
}

.action-btn {
    padding: 8px 20px;
    background: #1a73e8;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
        background: #1557b0;
        transform: translateY(-1px);
    }
}

.status-text {
    &.verified {
        color: #34a853;
        font-weight: 500;
        background: #e6f4ea;
        padding: 6px 12px;
        border-radius: 6px;
    }
}

.dialog-mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.dialog-container {
    width: 600px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.dialog-header {
    padding: 20px 24px;
    border-bottom: 1px solid #eee;
    display: flex;
    justify-content: space-between;
    align-items: center;

    h3 {
        font-size: 18px;
        font-weight: 500;
        color: #1a1a1a;
        margin: 0;
    }

    .close-btn {
        background: none;
        border: none;
        font-size: 24px;
        color: #999;
        cursor: pointer;
        padding: 0;
        line-height: 1;
        transition: color 0.3s;

        &:hover {
            color: #666;
        }
    }
}

.dialog-body {
    padding: 24px;
}

.dialog-footer {
    padding: 16px 24px;
    border-top: 1px solid #eee;
    display: flex;
    justify-content: flex-end;
    gap: 12px;

    button {
        padding: 8px 20px;
        border-radius: 4px;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.3s;
    }

    .cancel-btn {
        background: #fff;
        border: 1px solid #dcdfe6;
        color: #606266;

        &:hover {
            color: #409eff;
            border-color: #c6e2ff;
            background-color: #ecf5ff;
        }
    }

    .confirm-btn {
        background: #409eff;
        border: 1px solid #409eff;
        color: white;

        &:hover {
            background: #66b1ff;
            border-color: #66b1ff;
        }
    }
}

.password-form {
    width: 100%;
    .form-group {
        width: 100%;
        margin-bottom: 24px;

        &:last-child {
            margin-bottom: 0;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            color: #606266;
        }

        input {
            width: calc(600px - 78px);
            padding: 0 15px;
            height: 40px;
            line-height: 40px;
            border: 1px solid #dcdfe6;
            border-radius: 4px;
            transition: all 0.3s;

            &:focus {
                outline: none;
                border-color: #409eff;
            }

            &::placeholder {
                color: #c0c4cc;
            }
        }
    }
}

.password-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;

    input {
        width: 100%;
        padding-right: 40px; // 为图标留出空间
    }

    .password-toggle {
        position: absolute;
        right: 12px;
        top: 50%;
        transform: translateY(-50%);
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 20px;
        height: 20px;
        
        img {
            width: 100%;
            height: 100%;
            opacity: 0.6;
            transition: opacity 0.3s;
        }

        &:hover img {
            opacity: 1;
        }
    }
}
</style>
